package com.qipay.cp.impl;

import com.qipay.baas.model.Cp;
import com.qipay.baas.model.CpAccount;
import com.qipay.baas.model.CpZhiding;
import com.qipay.core.entity.Pagination;
import com.qipay.cp.CpService;
import com.qipay.mapper.CpMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class CpServiceImpl implements CpService {

    @Autowired
    CpMapper cpMapper;

    @Override
    public List<Cp> findAll(int p, int pz) {
        p = p <= 0 ? 1 : p;
        pz = pz <= 0 ? 10 : pz;
        return cpMapper.selectAll((p - 1) * pz, pz);
    }

    @Override
    public List<Cp> findAll() {
        return cpMapper.listAll();
    }

    @Override
    public List<CpAccount> allCpList(String name,Long cpId,String contact,String phone,Byte state,Long agentId,Pagination pagination) {
        return Pagination.wrapper(() -> cpMapper.allCpList(name,cpId,contact,phone,state,agentId), pagination);
    }

    @Override
    public List<CpAccount> allAgentList(String name, Long cpId, String person, String phone, Byte type, Long agentId, Pagination pagination) {
        return Pagination.wrapper(() -> cpMapper.allAgentList(name,cpId,person,phone,type,agentId), pagination);
    }

    @Override
    public CpAccount allCpListTj() {
        return cpMapper.CpAccountTj();
    }

    @Override
    public List<CpZhiding> findAllZd(int agentId) {
        return cpMapper.listAllZd(agentId);
    }

    @Override
    public Long count() {
        return cpMapper.countAll();
    }

    @Override
    public List<Cp> findByType(byte type, int p, int pz) {
        p = p <= 0 ? 1 : p;
        pz = pz <= 0 ? 10 : pz;
        return cpMapper.selectByType(type, (p - 1) * pz, pz);
    }

    @Override
    public Long countByType(byte type) {
        return cpMapper.countByType(type);
    }

    @Override
    public List<Cp> findByAgentId(long agentId, int p, int pz) {
        p = p <= 0 ? 1 : p;
        pz = pz <= 0 ? 10 : pz;
        return cpMapper.selectByAgentId(agentId, (p - 1) * pz, pz);
    }

    @Override
    public Long countByAgentId(long agentId) {
        return cpMapper.countByAgentId(agentId);
    }

    @Override
    public Cp getOneByAppId(String appId) {
        return cpMapper.selectByAppId(appId);
    }

    @Override
    public Cp getOneById(long id) {
        return cpMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int update(Cp cp) {
        return cpMapper.updateByPrimaryKey(cp);
    }

    @Override
    @Transactional
    public int insert(Cp cp) {
        cpMapper.insert(cp);
        Long cpId = cp.getId();
        //设置AppId跟登录CPId一致。
        Cp cpUpdate = new Cp();
        cpUpdate.setId(cpId);
        cpUpdate.setAppId(String.valueOf(cpId));
        cpMapper.updateByPrimaryKeySelective(cpUpdate);
        cp.setAppId(String.valueOf(cpId));
        return cpId != null ? cpId.intValue() : 0;
    }

    @Override
    @Transactional
    public int delete(long id) {
        return cpMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateAccountState(long cpId, byte state) {
        Cp cp = new Cp();
        cp.setId(cpId);
        cp.setAccountState(state);
        return cpMapper.updateByPrimaryKeySelective(cp);
    }

    @Override
    @Transactional
    public int updateContractState(long cpId, byte state) {
        Cp cp = new Cp();
        cp.setId(cpId);
        cp.setContractState(state);
        return cpMapper.updateByPrimaryKeySelective(cp);
    }

    @Override
    public boolean isIPValid(long cpId, String ip) {
        Cp cp = getOneById(cpId);
        if (StringUtils.isNotEmpty(cp.getIp())) {
            return Arrays.stream(cp.getIp().split(",")).anyMatch(p -> {
                return StringUtils.equals(p, ip);
            });
        }
        return true;
    }

    @Override
    public boolean isIPValid(long cpId, String ip, boolean force) {
        Cp cp = getOneById(cpId);
        if (StringUtils.isNotEmpty(cp.getIp())) {
            return Arrays.stream(cp.getIp().split(",")).anyMatch(p -> {
                return StringUtils.equals(p, ip);
            });
        }

        return !force;
    }

    @Override
    public List<Cp> findByAgentIdAndAccountState(Long agentId, Byte accountState) {
        return cpMapper.findByAgentIdAndAccountState(agentId, accountState);
    }

    @Override
    public int updatePwd(long cpId, String pwd) {
        Cp cp = new Cp();
        cp.setId(cpId);
        cp.setWithdrawPassword(pwd);
        return cpMapper.updateByPrimaryKeySelective(cp);
    }
}
